package com.wx;

import com.wx.entity.Menu;
import com.wx.entity.Menu.*;
import com.wx.restservice.Rest;
import com.wx.service.TextServiceType;
import com.wx.util.Util;
import com.wx.util.XmlUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @User: Jrxsr
 * Controller
 * on 2018/12/30.
 **/
@RestController
public class Controller {

    @Autowired
    Rest restImpl;

    @Autowired
    TextServiceType textServiceType;


    private Controller(){}
    private static Controller controller;
    public static synchronized Controller getInstance() {
        if(controller==null){
            controller = new Controller();
        }
        return controller;
    }

    @Value("${token-IP}")
    String tokenIP;
    @Value("${menu-IP}")
    String menuIP;
    @Value("${tokenval}")
    String tokenval;


    @GetMapping("token")
    public String get() {
        System.out.println(Setinterval.getInstance().getToken());
        /**
        map.put("grant_type","client_credential");
        map.put("appid","wx3bf01c8d32bd8c8d");
        map.put("secret","fde58d9488a703e8607cffd4d27b5c13");
         **/
        Setinterval setinterval = Setinterval.getInstance();
        setinterval.setRestImpl(restImpl);
        Thread thread = new Thread(Setinterval.getInstance());
        thread.start();
        String token = Setinterval.getInstance().getToken();
        return token;
    }

    @GetMapping("wx")
    public String wx(@RequestParam Map<String,String> map) {
        System.out.println(map.toString());
        String str = map.get("echostr");
        return str;
    }

    @PostMapping("wx")
    public String ww(HttpServletRequest request) throws IOException {
        Map<String,String> xml = XmlUtil.getXmlUtil().xmlToMap(request);
        System.out.println("获取到的信息："+xml.toString());
        String msgType = xml.get("MsgType");
        String xmlMsg = null;
        if ("text".equals(msgType)) { // 文本消息事件
            xmlMsg=textServiceType.handlealltext(xml);
        }else if("event".equals(msgType)){ // 事件
            xmlMsg=textServiceType.event(xml);
            String msg = xml.get("Event");
            switch (msg) {
                case "CLICK":
                    xmlMsg = textServiceType.click(xml);
                    break;
                case "subscribe"://关注事件
                    xmlMsg = textServiceType.event(xml);
                    break;
                default:
                    xml.put("EventKey","找不到");
                    xmlMsg = textServiceType.click(xml);
            }
        }else if("image".equals(msgType)) {
            xmlMsg = textServiceType.Imagemsg(xml);
        }

        return xmlMsg;
    }

    @GetMapping("createmenu")
    public String cm() {
        System.out.println(Setinterval.getInstance().getToken());
        Menu menu = new Menu();
        Menu.button button = new Menu().new button();
        Menu.sub_button sub_button = new Menu().new sub_button();
        sub_button.setName("投递信息");
        sub_button.setType("click");
        sub_button.setKey("投递信息暂不能使用");

        Menu.sub_button sub_button2 = new Menu().new sub_button();
        sub_button2.setName("面试信息");
        sub_button2.setType("view");
        sub_button2.setUrl("http://freenat.club:8181/");

        List<sub_button> buttons = new LinkedList<>();
        buttons.add(sub_button);
        buttons.add(sub_button2);
        button.setName("提交表单");
        button.setSub_button(buttons);

        Menu.button button2 = new Menu().new button();
        button2.setName("我要服务");
        button2.setType("view");
        button2.setUrl("http://www.ljxnb.xin");

        List<button> buttonList = new LinkedList<>();
        buttonList.add(button);
        buttonList.add(button2);

        menu.setButton(buttonList);
        System.out.println(JSONObject.fromObject(menu));
        String str = restful(JSONObject.fromObject(menu),menuIP+Setinterval.getInstance().getToken(),"post");
        System.out.println("cm方法返回："+str);
        return "success";
    }



    /*
     * obj    传入参数
     * method 请求的url地址
     * req    请求方式
     */
    public String restful(Object obj, String url, String req) {
        String str=null;
        ResponseEntity entity=null;
        switch (req){
            case "get":
                if(obj==null) {
                    System.out.println("controller:"+restImpl);
                    str = (String)restImpl.restget(url, String.class);
                }else{
                    str = (String)restImpl.restget(url, String.class, obj);
                }
                break;
            case "post":
                System.out.println("Here We Are Again POST");
                entity = (ResponseEntity) restImpl.restpost(url, obj, String.class);
                str = entity.getBody().toString();
                break;
            case "put":
                System.out.println("Here We Are Again PUT");
                str = (String)restImpl.restput(url, req);
                break;
            case "delete":
                str = (String)restImpl.restdelete(url, req);
                break;
            default: str = "未找到方法";
        }
        System.err.println(req+"请求,地址："+"["+url+"]");
        if (entity!=null){
            System.out.println("entity:"+entity.getBody().toString());
        }
        return str;
    }


}
